package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.RtMetricSyncConfig;
import com.hllxd.hism.star.basic.service.service.RtMetricSyncConfigService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author fangpeiyun
 * @date 2022.02.11
 * 监控大盘信息配置
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/rt_metric_config")
public class RtMetricSyncConfigController2 {

    @Autowired
    private RtMetricSyncConfigService rtMetricSyncConfigService;

    @Data
    public static class Params {
        public Long id;
        public Integer enable;
    }

    @RestGrpcMapping("/list.do")
    public NTSTATUS listPermission(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<RtMetricSyncConfig> items = rtMetricSyncConfigService.list();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/set.do")
    public NTSTATUS setPermission(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            RtMetricSyncConfig aa = params.driverObject(RtMetricSyncConfig.class);
            Object res = rtMetricSyncConfigService.set(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/enable.do")
    public NTSTATUS setEnable(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            Object res = rtMetricSyncConfigService.doEnable(aa.id, aa.enable);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/detail.do")
    public NTSTATUS detail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            RequestDefine aa = params.driverObject(RequestDefine.class);
            RtMetricSyncConfig res = rtMetricSyncConfigService.get(aa.getId());
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @Data
    private static class RequestDefine {
        public Long id;
    }

    @RestGrpcMapping("/delete.do")
    public NTSTATUS delete(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            RequestDefine aa = params.driverObject(RtMetricSyncConfigController2.RequestDefine.class);
            Object res = rtMetricSyncConfigService.del(aa.id);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
